﻿//var Domain = window.location.host;

var Domain = "http://apichat.vaubang.cn";

Domain = "https://localhost:44387";
var Authorization = localStorage.getItem("accessToken");


function FindLoginState(errstr) {
    if (errstr == "登录超时" || errstr == "未授权访问被拒绝" || errstr.indexOf("access_token") != -1) {
        window.alert("登录超时,请重新登录");
        window.location.href = Domain + "/Html2.1/login.html";
    }
    else if (errstr.indexOf("not external contact") != -1) {
        window.alert("你没有访问该功能的权限");
    }
    else {
        console.log("系统错误:"+errstr);
    }
}

function FindLoginState(errstr,errApi) {
    if (errstr == "登录超时" || errstr == "未授权访问被拒绝" || errstr.indexOf("access_token") != -1) {
        window.alert("登录超时,请重新登录");
        window.location.href = Domain + "/Html2.1/login.html";
    }
    else if (errstr.indexOf("未将对象") != -1)
    {
        console.log(errApi);
        console.log(errstr);
    }
    else {
        console.log(errApi);
        window.alert(errstr);
    }
}

function IndexObjOf(arr1, obj) {
    var result = true;
    arr1.forEach(item => {
        if (item.id == obj.id) {
            result= false;
        }
    })
    return result;
}

// 获取当前日期
function getDate() {
    var mydate = new Date();
    var str = "" + mydate.getFullYear() + "-";
    let month = "";
    let day = "";
    if (mydate.getMonth() < 10) {
        month = "0" + (mydate.getMonth() + 1);
    } else {
        month = mydate.getMonth() + 1;
    }
    if (mydate.getDate() < 10) {
        day = "0" + mydate.getDate();
    } else {
        day = mydate.getDate();
    }
    str += month + "-";
    str += day;
    return str;
}
// 获取当前日期 + 时间
function getDateTime() {
    var mydate = new Date();
    var str = "" + mydate.getFullYear() + "-";
    let month = "";
    let day = "";
    let hours = mydate.getHours(); // 获取当前小时数(0-23)
    let min = mydate.getMinutes(); // 获取当前分钟数(0-59)
    let sec = mydate.getSeconds(); // 获取当前秒数(0-59)
    if (mydate.getMonth() < 10) {
        month = "0" + (mydate.getMonth() + 1);
    } else {
        month = mydate.getMonth() + 1;
    }
    if (mydate.getDate() < 10) {
        day = "0" + mydate.getDate();
    } else {
        day = mydate.getDate();
    }
    str += month + "-";
    str += day + " ";
    str += hours + ":";
    str += min + ":";
    str += sec;
    return str;
}
// 昨天的日期
function getPreDate(dayNum) {
    var day1 = new Date();
    day1.setTime(day1.getTime() - (dayNum || 1) * 24 * 60 * 60 * 1000);
    let month = "";
    let day = "";
    if (day1.getMonth() < 10) {
        month = "0" + (day1.getMonth() + 1);
    } else {
        month = day1.getMonth() + 1;
    }
    if (day1.getDate() < 10) {
        day = "0" + day1.getDate();
    } else {
        day = day1.getDate();
    }
    var s1 = day1.getFullYear() + "-" + month + "-" + day;
    return s1;
}

// 指定日期的前多少天日期
function specifiedPreDate(date, dayNum) {
    var day1 = new Date(date);
    day1.setTime(day1.getTime() - (dayNum || 1) * 24 * 60 * 60 * 1000);
    let month = "";
    let day = "";
    if (day1.getMonth() < 10) {
        month = "0" + (day1.getMonth() + 1);
    } else {
        month = day1.getMonth() + 1;
    }
    if (day1.getDate() < 10) {
        day = "0" + day1.getDate();
    } else {
        day = day1.getDate();
    }
    var s1 = day1.getFullYear() + "-" + month + "-" + day;
    return s1;
}
// 指定日期的后多少天日期
function specifiedNextDate(date, dayNum) {
    var day1 = new Date(date);
    day1.setTime(day1.getTime() + (dayNum || 1) * 24 * 60 * 60 * 1000);
    let month = "";
    let day = "";
    if (day1.getMonth() < 10) {
        month = "0" + (day1.getMonth() + 1);
    } else {
        month = day1.getMonth() + 1;
    }
    if (day1.getDate() < 10) {
        day = "0" + day1.getDate();
    } else {
        day = day1.getDate();
    }
    var s1 = day1.getFullYear() + "-" + month + "-" + day;
    return s1;
}

function downloadModel(postdata, url, ajaxType, modelName) {
    let xhr = new XMLHttpRequest()
    xhr.responseType = "blob"
    xhr.open(ajaxType, url, true)
    xhr.setRequestHeader("Content-type", "application/json")
    xhr.setRequestHeader("Authorization", Authorization)
    xhr.onload = function () {
        const blob = this.response
        const objectUrl = URL.createObjectURL(blob)
        const a = document.createElement("a")
        document.body.appendChild(a)
        a.style = "display: none"
        a.href = objectUrl
        a.download = modelName+'.xlsx'
        a.click()
        document.body.removeChild(a)
        return
    }
    xhr.send(JSON.stringify(postdata))
}
//免验证 get 获取下载文件
function getModelGet(url, modelName) {
    var xhr = new XMLHttpRequest();
    xhr.open('Get', url, true);        // 也可以使用POST方式，根据接口
    xhr.responseType = "blob";    // 返回类型blob
    xhr.onload = function () {
        if (this.status === 200) {
            var blob = this.response;
            var reader = new FileReader();
            reader.readAsDataURL(blob);    // 转换为base64，可以直接放入a表情href
            reader.onload = function (e) {
                var a = document.createElement('a');
                a.download = modelName + '.xlsx';//下载文件名
                a.href = e.target.result;
                console.log(blob);
                $("body").append(a);  // 修复firefox中无法触发click
                a.click();
                $(a).remove();
            }
        }
    };
    // 发送ajax请求
    xhr.send()
}
//get 获取下载文件
function downloadModelGet(url, modelName) {
    var xhr = new XMLHttpRequest();
    xhr.open('Get', url, true);        // 也可以使用POST方式，根据接口
    xhr.responseType = "blob";    // 返回类型blob
    xhr.setRequestHeader("Authorization", Authorization)
    xhr.onload = function () {
        if (this.status === 200) {
            var blob = this.response;
            var reader = new FileReader();
            reader.readAsDataURL(blob);    // 转换为base64，可以直接放入a表情href
            reader.onload = function (e) {
                var a = document.createElement('a');
                a.download = modelName+'.xlsx';//下载文件名
                a.href = e.target.result;
                console.log(blob);
                $("body").append(a);  // 修复firefox中无法触发click
                a.click();
                $(a).remove();
            }
        }
    };
    // 发送ajax请求
    xhr.send()
}
    